package com.lrkj.property.controller;

import com.lrkj.common.annotation.Log;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.utils.poi.ExcelUtil;
import com.lrkj.property.domain.Unit;
import com.lrkj.property.service.IUnitService;
import com.lrkj.property.vo.UnitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单元信息Controller
 *
 * @author yajun_ren
 * @date 2021-07-22
 */
@Api(tags = "单元信息")
@RestController
@RequestMapping("/property/unit")
public class UnitController extends BaseController {
    @Autowired
    private IUnitService unitService;

    /**
     * 查询单元信息列表
     */
    @ApiOperation(value = "查询单元信息列表", notes = "查询单元信息列表")
    @PreAuthorize("@ss.hasPermi('property:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(Unit unit) {
        startPage();
        List<Unit> list = unitService.selectUnitList(unit);
        return getDataTable(list);
    }

    /**
     * 导出单元信息列表
     */
    @ApiOperation(value = "导出单元信息列表", notes = "导出单元信息列表")
    @PreAuthorize("@ss.hasPermi('property:unit:export')")
    @Log(title = "单元信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Unit unit) {
        List<Unit> list = unitService.selectUnitList(unit);
        ExcelUtil<Unit> util = new ExcelUtil<Unit>(Unit.class);
        return util.exportExcel(list, "单元信息数据");
    }

    /**
     * 获取单元信息详细信息
     */
    @ApiOperation(value = "获取单元信息详细信息", notes = "获取单元信息详细信息")
    @PreAuthorize("@ss.hasPermi('property:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(unitService.selectUnitById(id));
    }

    /**
     * 新增单元信息
     */
    @ApiOperation(value = "新增单元信息", notes = "新增单元信息")
    @PreAuthorize("@ss.hasPermi('property:unit:add')")
    @Log(title = "单元信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Unit unit) {
        return toAjax(unitService.insertUnit(unit));
    }

    /**
     * 修改单元信息
     */
    @ApiOperation(value = "修改单元信息", notes = "修改单元信息")
    @PreAuthorize("@ss.hasPermi('property:unit:edit')")
    @Log(title = "单元信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Unit unit) {
        return toAjax(unitService.updateUnit(unit));
    }

    /**
     * 删除单元信息
     */
    @ApiOperation(value = "删除单元信息", notes = "删除单元信息")
    @PreAuthorize("@ss.hasPermi('property:unit:remove')")
    @Log(title = "单元信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(unitService.deleteUnitByIds(ids));
    }


    /**
     * 新增单元数量
     */
    @ApiOperation(value = "新增单元数量", notes = "新增单元数量")
    @PreAuthorize("@ss.hasPermi('property:unit:addUnitNum')")
    @PostMapping("/addUnitNum")
    public AjaxResult addUnitNum(@RequestBody UnitVo unitVo) {
        return toAjax(unitService.addUnitNum(unitVo));
    }
}
